import { getCreateList } from '@/api/common';
import { getAsyncTaskModule, getAsyncFunction } from '@/api/channelconfig/asyncTask';
import { reactive } from 'vue';

export const mainOptionList = reactive<any[]>([]);
export const subOptionList = reactive<any[]>([]);

export default [
    {
        label: '标题',
        name: 'title',
        value: '',
        type: 'input',
        placeholder: '请输入标题'
    },
    {
        label: '编号',
        name: 'async_no',
        value: '',
        type: 'input',
        placeholder: '请输入编号'
    },
    {
        label: '操作者',
        name: 'created_ids',
        value: [],
        multiple: true,
        type: 'select',
        filterable: true,
        placeholder: '请选择操作者',
        fetchOptionApi: () => {
            return new Promise(resolve => {
                getCreateList({}).then((res: any) => {
                    const targetArr = res.map((item: any) => {
                        item.name = `${item.name}  (${item.phone})`;
                        return item;
                    });
                    resolve(targetArr);
                });
            });
        },
        formatOption: (v: any) => ({
            label: v.name,
            value: v.id
        })
    },
    {
        label: '状态',
        name: 'task_state',
        value: '0',
        type: 'select',
        placeholder: '请选择状态',
        optionList: [
            { label: '全部', value: '0' },
            { label: '等待', value: 1 },
            { label: '进行中', value: 2 },
            { label: '已取消', value: 5 },
            { label: '已失效', value: 6 },
            { label: '已完成', value: 3 },
            { label: '异常', value: 4 }
        ]
    },
    {
        label: '开始时间',
        name: 'begin_time',
        value: [],
        type: 'datePicker',
        datePickerType: 'daterange',
        datePickerFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        startPlaceholder: '开始日期',
        endPlaceholder: '结束日期'
    },
    {
        label: '完成时间',
        name: 'finish_time',
        value: [],
        type: 'datePicker',
        datePickerType: 'daterange',
        datePickerFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        startPlaceholder: '开始日期',
        endPlaceholder: '结束日期'
    },
    {
        label: '功能类型',
        name: 'type',
        value: '',
        type: 'select',
        placeholder: '请选择功能类型',
        fetchOptionApi: () => {
            return new Promise(resolve => {
                getAsyncFunction().then((res: any) => {
                    mainOptionList.splice(0);
                    mainOptionList.push(...res);
                    resolve([{ id: 0, name: '全部' }, ...(res || [])]);
                });
            });
        },
        change: (id: number, form: any) => {
            form['sub_type'] = '';
            subOptionList.splice(0);
            const targetItem = mainOptionList.find(item => {
                return item.id === id;
            });
            if (targetItem && targetItem.sub_function) {
                subOptionList.push(...targetItem.sub_function);
            }
        },
        formatOption: (v: any) => ({
            label: v.name,
            value: v.id
        })
    },
    {
        label: '子功能类型',
        name: 'sub_type',
        value: '',
        type: 'select',
        placeholder: '请选择子任务功能',
        optionList: subOptionList
    },
    {
        label: '所属模块',
        name: 'module_type',
        value: '',
        type: 'select',
        filterable: true,
        placeholder: '请选择所属模块',
        fetchOptionApi: () => {
            return new Promise(resolve => {
                getAsyncTaskModule().then((res: any) => {
                    resolve([{ id: 0, name: '全部' }, ...(res || [])]);
                });
            });
        },
        formatOption: (v: any) => ({
            label: v.name,
            value: v.id
        })
    }
];
